home *** CD-ROM | disk | FTP | other *** search
/ Risc World 3 / Risc World 3.iso / SOFTWARE / ISSUE6 / PD / PDF / pdf / c++ / UriDispatch < prev   
Text File  |  2003-02-14  |  5KB  |  159 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //   Copyright (c) 2002, Colin Granville
  4. //
  5. //   All rights reserved.
  6. //
  7. //   Redistribution and use in source and binary forms, with or
  8. //   without modification, are permitted provided that the following 
  9. //   conditions are met:
  10. //
  11. //      * Redistributions of source code must retain the above copyright 
  12. //        notice, this list of conditions and the following disclaimer.
  13. //
  14. //      * Redistributions in binary form must reproduce the above 
  15. //        copyright notice, this list of conditions and the following 
  16. //        disclaimer in the documentation and/or other materials 
  17. //        provided with the distribution.
  18. //
  19. //      * The name Colin Granville may not be used to endorse or promote 
  20. //        products derived from this software without specific prior 
  21. //        written permission.
  22. //
  23. //   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  24. //   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  25. //   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
  26. //   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
  27. //   COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
  28. //   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  29. //   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  30. //   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  31. //   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
  32. //   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  33. //   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
  34. //   OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. //--------------------------------------------------------------------------
  37.  
  38. #include "UriDispatch.h"
  39. #include "swis.h"
  40. #include <stdlib.h>
  41. #include <ctype.h>
  42. #include "GuiTargets.h"
  43.  
  44. class UriHandler
  45. {
  46.   public:
  47.     UriHandler();
  48.     ~UriHandler();
  49.     bool dispatch(const string& uri);
  50.  
  51.   private:
  52.     int my_ref;
  53.  
  54.     GUI_DECLARE_EVENT_TARGETS(UriHandler);
  55.  
  56.     GuiWimpTarget ant_uri_failed_target;
  57.     Claim ant_uri_failed(GuiWimpPollBlock&,const GuiIdBlock&);
  58. };
  59.  
  60. //*************************************************************************
  61.  
  62. union AntStringValue
  63. {
  64.   char *ptr;
  65.   int offset;
  66. };
  67.  
  68. //*************************************************************************
  69.  
  70. struct AntURLMessageBlock : public GuiWimpMessageBase
  71. {
  72.   union
  73.   {
  74.     char url[236];
  75.     struct
  76.     {
  77.       int tag;
  78.       AntStringValue url;
  79.       int flags;
  80.       AntStringValue body_file;
  81.       AntStringValue target;
  82.       AntStringValue body_mimetype;
  83.     } indirect;
  84.   } data;
  85. };
  86.  
  87. //*************************************************************************
  88.  
  89. UriHandler::UriHandler()
  90.   : my_ref(-1),
  91.     ant_uri_failed_target(0,GuiWimp_EUserMessageAcknowledge,this,UriHandler::ant_uri_failed)
  92. {
  93. }
  94.  
  95. //*************************************************************************
  96.  
  97. UriHandler::~UriHandler()
  98. {
  99. }
  100.  
  101. //*************************************************************************
  102.  
  103. bool UriHandler::dispatch(const string& uri_string)
  104. {
  105.   string uri=uri_string;
  106.   int i;
  107.   for (i=0;i<uri.size() && !isalpha(uri[i]);i++); //empty loop
  108.   uri.erase(0,i);
  109.   if (!uri.size()) return 0;
  110.   for (i=uri.size()-1;!(isalnum(uri[i]) || uri[i]=='/' || uri[i]=='-' || uri[i]=='_');i--); //empty loop
  111.   uri.resize(i+1);
  112.   if (uri.find("://")!=string::npos) {/*empty*/}
  113.   else if (uri.compare(0,4,"www.")==0) uri.insert(0,"http://");
  114.   else if (uri.compare(0,4,"ftp.")==0) uri.insert(0,"ftp://");
  115.   else if (uri.compare(0,7,"mailto:")==0) {/*empty*/}
  116.   else if (uri.compare(0,5,"news:")==0) {/*empty*/}
  117.   else if (uri.compare(0,5,"nntp:")==0) {/*empty*/}
  118.   else if (uri.find('@')!=string::npos) uri.insert(0,"mailto:");
  119.   else return 0;
  120.  
  121.   if (!_swix(0x4e381 /*Acorn URI_Dispatch*/,_INR(0,2),0,uri.c_str(),0)) return 1;
  122.  
  123.   AntURLMessageBlock mess;
  124.   mess.hdr.size=0;//keep compiler quiet
  125.   mess.copyString(mess.data.url,uri.c_str());
  126.   my_ref=mess.sendRecorded(0x4af80); //AntUrlMessage
  127.   return 1;
  128. }
  129.  
  130. //*************************************************************************
  131.  
  132. Claim UriHandler::ant_uri_failed(GuiWimpPollBlock& wpb,const GuiIdBlock&)
  133. {
  134.   if (wpb.userMessageAcknowledge.hdr.myRef!=my_ref) return DONT_CLAIM;
  135.   my_ref=-1;
  136.   AntURLMessageBlock& mess=(AntURLMessageBlock&) wpb;
  137.   int i;
  138.   for (i=0;isalpha(mess.data.url[i]);i++);//empty loop
  139.   string var;
  140.   var="Alias$URLOpen_";
  141.   var.append(mess.data.url,i);
  142.   if (getenv(var.c_str()))
  143.   {
  144.     var.erase(0,6);
  145.     var+=' ';
  146.     var+=mess.data.url;
  147.     _swix(Wimp_StartTask,_IN(0),var.c_str());
  148.   }
  149.   return CLAIM;
  150. }
  151.  
  152. //*************************************************************************
  153.  
  154. bool dispatch_uri(const string& uri)
  155. {
  156.   static UriHandler h;
  157.   return h.dispatch(uri);
  158. }
  159.